const api = require('../../../api/index.js')
let app = getApp();
var adds = {};
Page({
  data: {
    issueClass:'',
    catalogSelect: 0,
    img_arr: [],
    arr:[],
    formdata: '',
    imgI:[],
    sub:false,
    feedback_text:[
      {
        name: '登陆问题',
        "select":1,
        color:false
      },{
        name: '账号异常',
        "select":2,
        color: false
      },{
        name: '系统问题',
        "select":3,
        color: false
      },{
        name:'界面设计',
        "select":4,
        color: false
      },{
        name: '订单问题',
        "select": 5,
        color: false
      },{
        name:'场馆问题',
        "select": 6,
        color: false
      },{
        name: '客服问题',
        "select": 7,
        color: false
      }, {
        name: '改进建议',
        "select": 8,
        color: false
      }, {
        name: '其他',
        "select": 9,
        color: false
      }
    ],
    color:[],
    feedbackData: [],
    submitData: {}
  },
  onLoad:function(){

    App.zhuge.track('问题反馈', {});
    let _this = this;
    wx.request({
      url: api.baseURL+'/Smallprogram/Users/problem',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync('token').token || {}
      },
      method: 'POST',
      success: function (res) {
        let Arr = [];
        for (let i in res.data.result) {
          Arr.push({
            color: false,
            name: res.data.result[i],
            select: i
          })
        }
        _this.data.submitData.users_id = Arr[Arr.length - 2].name;  // 将用户id赋值给提交数据
        console.log(Arr);
        _this.setData({
          feedbackData: Arr
        })
      },
      fail: function (err) {
        callback(err)
      }
    })
  },
  calling: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.feedbackData[this.data.feedbackData.length - 1].name, //此号码并非真实电话号码，仅用于测试  
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  }, 
  oSelect:function(e){
    
    console.log(e.target.dataset.select)
    var num = e.target.id;
    var name = e.target.dataset.name;
    this.setData({
      issueClass:name
    })
    this.setData({//把选中值放入判断值
      catalogSelect: e.currentTarget.dataset.select
    })

  }, 
  formSubmit: function (e) {
    var id = e.target.id
    adds = e.detail.value;
    adds.program_id = app.jtappid
    adds.openid = app._openid
    adds.zx_info_id = this.data.zx_info_id
    this.upload(id)
  },
 
  upload: function (id) {
    console.log(id);
    var that = this
    for (var i = 0; i < this.data.img_arr.length; i++) {
      
      wx.uploadFile({
        url: api.baseURL +'/Smallprogram/Users/proposal',
        filePath: that.data.img_arr[i],
        name: 'content',
        formData: {
          count: that.data.img_arr.length,
          help_name:id,
          help_label: this.data.issueClass
        },
        success: function (res) {
          console.log(res)
        }  
      })
    }
    this.setData({
      formdata: ''
    })
    this.setData({
      issueClass: ''
    })
    wx.showToast({
      title: '已提交发布！',
      duration: 3000
    });
    setTimeout(function () {
      wx.navigateBack({
        delta: 1
      })
    }, 1000)
  },
  deletes: function (e) {
    let index_img = e.target.dataset.index;
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.log(index);
    var imgList = this.data.img_arr;
    console.log(imgList)
    var arr = that.data.arr;
    imgList.splice(index, 1);
    arr.splice(index, 1);
    var num = that.data.i - 1
    that.setData({
      img_arr: imgList,
      arr: arr,
      imgI: num,
    });
  },
  upimg: function () {
    var that = this;
    if (this.data.img_arr.length < 3) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths)
          })
        }
      })
    } else {
      wx.showToast({
        title: '最多上传三张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },
  textAreaText: function(e){
    this.data.submitData.proposal = e.detail.value
    this.setData({
      formdata: e.detail.value
    })
  }, 
  submitData: function(){
    console.log(this.data.formdata);
    let feedBackData = this.data.submitData;
    let that = this;
    console.log('我要上天了')
    let  fData = {
      help_name: that.data.formdata,
      help_label: that.data.issueClass
    }  

    if (feedBackData.proposal != undefined || this.data.issueClass != ''){
      api.feedback(fData, (res) => {
        this.upload(res.data.result.id) 
      })
    } else{
      wx.showToast({
        title: '请输入反馈内容',
        icon: 'none',
        duration: 1000
      });
    }
  },
  dataOver: function () {
    console.log(1)
  },
}) 